package cn.xyinping.org.service.impl;

import cn.xyinping.basic.exception.MyException;
import cn.xyinping.basic.service.impl.BasicServiceImpl;
import cn.xyinping.org.domain.Employee;
import cn.xyinping.org.domain.Shop;
import cn.xyinping.org.mapper.EmployeeMapper;
import cn.xyinping.org.mapper.ShopMapper;
import cn.xyinping.org.service.IEmployeeService;
import cn.xyinping.org.service.IShopService;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 店铺管理
 * @author 27266
 * @desrciption
 * @date 2021/10/17
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BasicServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    public void ShopRegister(Shop shop) {
        Employee admin = shop.getAdmin();
        // 两次密码是否一致
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new MyException("两次密码不一致");
        }
        // 用户名是否存在
        Employee employee = employeeMapper.findById(admin.getUsername());
        if(employee!=null){
            throw new MyException("用户已存在");
        }
        // 两张表（show,employee）先添加employee返回自增长id
        employeeMapper.add(admin);
        // 再把employee的自增id设置到shop中，添加Shop
        shop.setAdmin(admin);
        shopMapper.add(shop);
        admin.setShop_id(shop.getId());
        // 更新employye表中show_id字段
        employeeMapper.update(admin);
    }
}
